package com.ys.peony.http.base;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.ys.peony.app.YSConstant;
import com.ys.peony.encrypt.AESUtils;
import com.ys.peony.utils.JSONUtil;
import com.ys.peony.utils.LoadingProgressUtils;
import com.ys.peony.utils.LogUtil;
import com.ys.peony.utils.StringUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import okhttp3.internal.http.RealResponseBody;
import okio.BufferedSource;

/**
 * 请求后返回数据回调处理
 * <p>
 * <p>
 * Created by yml on 16/6/15.
 */
public class BaseRespCallBack implements Callback {
    private final static String TAG = "BaseRespCallBack";
    public final static int TAG_NO_DISMISS_DIALOG = 1;//对于一些情况 关闭对话框放在view中处理
    private Activity activity = null;
    private Handler mHandler = new Handler(Looper.getMainLooper());

    private int mTag = YSConstant.INVALID;

    public BaseRespCallBack() {
    }

    public BaseRespCallBack(int tag) {
        mTag = tag;
    }

    public BaseRespCallBack(Activity activity) {
        this.activity = null;
    }

    @Override
    public void onFailure(Call call, final IOException e) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                LoadingProgressUtils.closeDialog();
                e.printStackTrace();
                onRespFailure(StringUtils.replaceBlank("请求失败"));
            }
        });
    }

    @Override
    public void onResponse(Call call, final Response response) throws IOException {

        BufferedSource result = null;
        try {
            result = ((RealResponseBody) (response.body())).source();
            final String respStr = result.readUtf8();

            if (mTag == YSConstant.INVALID) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        LoadingProgressUtils.closeDialog();
                        LogUtil.i(TAG, "返回参数:" + respStr);
                        if (!JSONUtil.isJsonFormat(respStr)) {
                            onRespFailure("数据请求错误");
                            return;
                        }
                        final BaseRespBean resp = JSONUtil.parseObject(respStr, BaseRespBean.class);
                        if (resp != null && !StringUtils.isEmpty(resp.getResult())) {
                            if (YSConstant.HTTP_SUCCESS.equals(resp.getResult())) {
                                if (resp.getData() != null) {
                                    onRespSuccess(AESUtils.Decrypt(resp.getData().toString()));
                                    LogUtil.i(TAG, "数据data:" + AESUtils.Decrypt(resp.getData().toString()));
                                } else {
                                    onRespSuccess("成功");
                                }

                            } else {
                                onRespFailure(resp.getMessage());
                            }
                        } else {
                            onRespSuccess(respStr);
                        }
                    }
                });
            } else {
                LogUtil.i(TAG, "返回参数:" + respStr);
                if (!JSONUtil.isJsonFormat(respStr)) {
                    onRespFailure("数据请求错误");
                    return;
                }
                final BaseRespBean resp = JSONUtil.parseObject(respStr, BaseRespBean.class);
                if (resp != null && !StringUtils.isEmpty(resp.getResult())) {
                    if (YSConstant.HTTP_SUCCESS.equals(resp.getResult())) {
                        onRespSuccess(AESUtils.Decrypt(resp.getData().toString()));
                        LogUtil.i(TAG, "数据data:" + AESUtils.Decrypt(resp.getData().toString()));

                    } else {
                        onRespFailure(resp.getMessage());
                    }
                } else {
                    onRespSuccess(respStr);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "exception->" + e);
        }


    }

    public void onRespFailure(String msg) {

    }

    public void onRespSuccess(String responseStr) {

    }

}
